/**
 * 全局获取异步请求封装
 */
import $_fn from '~plugins'
import config from '~src/config.js';
class http {
    constructor($ui,$location) {
        this.$ui = $ui
        this.$location = $location
        this.Msg = function (text) {
            $ui.toast({text:text,type:'warning',position:'center',appendTo:'.wp-container'});
        }
    }
    /*
    * url        请求地址
    * param      传入参数，非必填，没参数可直接写回调
    * callBack   获取数据成功回调
    * errorCall  获取数据错误回调
    * Loading    loading
    * */
    post (url, param, callBack, errorCall, Loading) {
        if(typeof param == 'function'){
            callBack = param
            errorCall = callBack
            Loading = errorCall
            param = {}
        }
        var isLoading = Loading || false
        var layer = null
        var _this  = this
        $.support.cors = true
        if (!isLoading) {
            layer =  new $_fn.layer()
            layer.show()
        }
        $.ajax({
            type: 'POST',
            url:config.DEV_API_URL +'/manager/' + url,
            data: param,
            xhrFields: {
                withCredentials: true
            },
            // contentType: "application/json; charset=utf-8",//必须声明contentType为application/json,否则后台使用@RequestBody标注的话无法解析参数
            dataType: "json",
            crossDomain: true,
            success: function (data) {
                if (!isLoading)layer.hide()
                if (data == 'noLogin') {
                    //未登录处理
                    _this.$location.path('/login')
                    _this.Msg('请先登录')
                    return
                }
                if (data == 'noRole') {
                    //未登录处理
                    _this.Msg('您没有权限访问当前资源')
                    return
                }
                var type = data.type;
                if (type==-111){
                    window.location.href=data.url;
                }

                callBack && callBack(data)
            },
            error: function (XMLHttpRequest) {
                if (!isLoading){
                    layer.hide()
                }
                if (XMLHttpRequest.status == 0 || XMLHttpRequest.status == 504 || XMLHttpRequest.status == 500 || XMLHttpRequest.status == 400) {
                    _this.Msg('服务器无响应,Error:'+XMLHttpRequest.status)
                }
                //如请求错将错误信息输出到当前页面
                errorCall && errorCall()
                return
            }
        })
    }
    post1 (url, param, callBack, errorCall,a ,Loading) {
        if(typeof param == 'function'){
            callBack = param
            errorCall = callBack
            Loading = errorCall
            param = {}
        }
        var isLoading = Loading || false
        var layer = null
        var _this  = this
        $.support.cors = true
        if (!isLoading) {
            layer =  new $_fn.layer()
            layer.show()
        }
        //参数加密
        /*var encrypt1 = new JSEncrypt();//公钥对象
		var encrypt2 = new JSEncrypt();//私钥对象
		//公钥
		encrypt1.setPublicKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCo9yyyvSUdMIu+Q3FniJuzFSfYyOc0UHZ/JGyWqwTlIgWxyAm6AcyRTzP//L6JdQ6+u8QSRWTmWW+dMSeGm0tYVAobGvTGG1eIduhE1RMqGYDbHzlCzTgkfvMIEpRncjzQYMDtlXH+i+sVeIEgM+eJnnCgRte5vicHKKkFmlg8LQIDAQAB");
		//私钥
		encrypt2.setPrivateKey('MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKfrKjZyCf8tI52u6LEwLMgmOKdNhqjVrNhnL3c9+pU0KRYKQrmDDf7R/jI7ZTI92luTUFw4PF1AJJgkgMRWQveZryD465lLX2M+Y6QasMk/VOJtdU+iZ1TQOeCzS//Hr3GpRYHUL6Q2oUsoDRgP+bvi13ypS8BnlvhxCxyCZi2VAgMBAAECgYBurd2kB2S5ckpAe/JaIBPUVQUQmVQB4t1Lwi+SHCMXx0qxgU/bsRs/c7Bl8apRBF3x8FaOgm6fVFEKizzorKb0M8MiHhp5FjLmg5+lPMgylaYSBQbEckFGBtPx5LXG+LdRU+VIi+RJtye4xzk2URSOk6iBBuykWknZpBlrcJ9e4QJBAOck0+PaZUjbaAf2y4e6i21OPkyqkbf58R0rmva9WVcXX3rgQtj5qXJqcB501NlLaz2fVb1cwcoxqyVCM63OGckCQQC5+c5MexsOU6pi15YJWw1fnrsOeA+Lg0MLcn6N7MGgfrG2Bygobgb6X1tKKAbAWbl5n+4UXvWQ0yKaXiAkBRttAkAFxgi450qiYECf+k0pLcKvz0WFGrmbE9Yu+Vjckuw5Xa8X3aQt0UanQftc92QdrWGXX3R/z2JTz5vLyXSLAdvpAkBfz11Ds4xNxjaac1x9d2YdY+YUXzAK/NW2eQbP0TAzofGlyGwc4ApCRIwAWTyZLeBK02TRI9a5BLzn8CP4ypTFAkEAq4tAs14vKDlR97V85Tq5mUXoAjEO2I2rFX+44uqkXlESkhStW2ML93j9ziU8HMhoETQ287Ze7hTYiLv4h54E1g==')
        for(name in param){
			param[name]=encrypt1.encrypt(param[name]+'')
		}*/
        $.ajax({
            type: 'POST',
            url:config.DEV_API_URL1 +'/sys/CarloanCheck/' + url,
            data: param,
            xhrFields: {
                withCredentials: true
            },
            async:a,
            // contentType: "application/json; charset=utf-8",//必须声明contentType为application/json,否则后台使用@RequestBody标注的话无法解析参数
            dataType: "json",
            crossDomain: true,
            success: function (data) {
                if (!isLoading)layer.hide()
                if (data == 'noLogin') {
                    //未登录处理
                    _this.$location.path('/login')
                    _this.Msg('请先登录')
                    return
                }
                if (data == 'noRole') {
                    //未登录处理
                    _this.Msg('您没有权限访问当前资源')
                    return
                }
                callBack && callBack(data)
            },
            error: function (XMLHttpRequest) {
                if (!isLoading){
                    layer.hide()
                }
                if (XMLHttpRequest.status == 0 || XMLHttpRequest.status == 504 || XMLHttpRequest.status == 500 || XMLHttpRequest.status == 400) {
                    _this.Msg('服务器无响应,Error:'+XMLHttpRequest.status)
                }
                //如请求错将错误信息输出到当前页面
                errorCall && errorCall()
                return
            }
        })
    }
    /*
    * 没有manager
    * url        请求地址
    * param      传入参数，非必填，没参数可直接写回调
    * callBack   获取数据成功回调
    * */
    _post (url, param, callBack) {
        var _this = this
        if(typeof param == 'function'){
            callBack = param
            param = {}
        }
        $.support.cors = true
        $.ajax({
            type: 'post',
            url:config.DEV_API_URL+'/'+ url,
            dataType: 'json',
            data: param,
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (data) {
                //接口没有权限、未登录处理
                if (data == 'noLogin' || data == 'noRole') {
                    _this.$location.path('/login')
                    return
                }
                callBack(data)
            },
            error: function () {
                if (XMLHttpRequest.status == 0 || XMLHttpRequest.status == 504 || XMLHttpRequest.status == 500 || XMLHttpRequest.status == 400) {
                    _this.Msg('错误码-'+XMLHttpRequest.status+',服务器没有响应')
                }
            }
        })
    }
}

http.$inject = ['$ui','$location'];
export default angular.module('app.http', [])
    .service('http', http)
    .name;
